import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router)
import LevPageRouter from '@/module/LevThrPage/router/LevRouter'
  const routes = [
  // 激活路由

  // 登录
  
  { path: '/login', component: () => import('@/components/page/Login.vue')},
  {path: '/Forget', component: () => import('@/components/page/Forget.vue')},
  {path: '/QR', component: () => import('@/components/page/QR.vue')},
  {path: '/Reg', component: () => import('@/components/page/Reg.vue')},
  {path: '/Views', component: () => import('@/components/common/View.vue'),meta:{bool:true}},

  { path: '/', component: () => import('@/components/common/Home.vue'),
  children:[
    {path: '/dashboard',alias:'/', component: () => import('@/components/page/Dashboard.vue'),meta:{bool:true}},
    {path: '/dashuse', component: () => import('@/components/page/Dashuse.vue'),meta:{bool:true}},
    {path: '/LevHome', component: () => import('@/module/LevThrPage/page/LevHome.vue'),meta:{bool:true},children: [...LevPageRouter]},
  ]},
  { path: '/404', component: () => import('@/components/page/404.vue') },
  { path: '/aaa', component: () => import('@/components/common/aaa.vue') },
]

const router = new Router({
  mode: 'hash', 
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
// 也就是监控路由数据变化
// 只要变化了就代表你访问了新的页面 
// 接着你就判断有没有登录 
router.beforeEach((to, from, next) => {
  if (to.meta.bool) {
    next()
    if (localStorage.getItem('User')) {
      next()
    } else {
      next({ path: '/login' })
    }
  } else {
    next()
  }
})

// router.beforeEach((newData,oldData,next)=>{
//     // 判断是否登录
//     // 如果上次
//     if(newData.path=='/login' || newData.path=='/404'){
//       next()
//     }else{
//       let uname=localStorage.getItem('uname')
//       let token=localStorage.getItem('token')
//       if(uname && token){
//         next()
//       }else{
//         next({path:'/login'})
//       }
//     };
//   })
export default router
